import os


import requests
from pyquery import PyQuery as pq
def bian_w(page):
    if page == 1:
        url = "https://pic.netbian.com/4kmeinv/index.html"
    else:
        url = f'https://pic.netbian.com/4kmeinv/index_{page}.html'
    # 模拟浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.57',
        'Referer': 'https://pic.netbian.com/4kmeinv/'


    }
    r = requests.get(url=url, headers=headers).content.decode(encoding='gbk')
    # print(r)
    # open('1.html','w').write(r)
    ret = pq(r) #pyquery类型
    li_list = ret('ul.clearfix>li')
    # print(li_list)
    index = 1
    for li in li_list:
        # 获取所有的图片的详情页网址
        href = pq(li)('a').attr('href')
        # '/uploads/allimg/230302/004645-16776892053278.jpg'
        # print(href)
        # 获取详情页源代码
        detail_r = requests.get(url='https://pic.netbian.com'+href, headers=headers).text
        detail_ret = pq(detail_r)
        # 获取下载地址
        div_list = detail_ret('div.view div ')
        img = div_list.children().eq(1)('a')('img').attr('src')
        end = img.split('.')[-1]
        # 保存图片，写入二进制数据
        img = requests.get(url='https://pic.netbian.com' + img, headers=headers).content
        print(f'彼岸网/{page}-{index}.{end}')
        with open(f'{word}/{page}-{index}.{end}', 'wb') as f:
            f.write(img)
        index+=1
# 导入文件夹
if __name__ == '__main__':
    word = '彼岸网'
    if not os.path.exists(word):
        os.mkdir(word)
for page in range(1,6):
    bian_w(page)